package com.hitrust.gs.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import system.base.Warehouse;
import system.base.WarehouseSearchEvent;
import system.data.CompanyMessageData;
import system.data.ProductTaxData;
import system.data.WarehouseBase;

import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.SQLLogger;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.lib.db.GSPreparedStatementFactory;
import com.hitrust.gs.util.exception.GSException;

public class GSCompanyMessageDao extends GSBaseDAO{
	
	/**
     * 查询营运公司客户相关信息
     * @throws GSException
     */
    public List queryCompanyMessage(String warehouseCode) throws GSException{
    	List ls = null;
        String tSql = "select a1.*,a2.name,a2.code from t_warehouse a2 left join t_company_message a1 on a1.warehouse_code = a2.code  where a2.code = ?";
        Object tObj[] = {warehouseCode};
        try{
            ls = executeQuery(tSql,tObj);          
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return ls;
    }
    
    
    /**
     * 查询营运公司是否已经有税务信息
     * @throws GSException
     */
    public List queryCompanyMessageIsOk(String warehouseCode) throws GSException{
    	List ls = null;
        String tSql = "select * from t_company_message where warehouse_code = ?";
        Object tObj[] = {warehouseCode};
        try{
            ls = executeQuery3(tSql,tObj);          
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return ls;
    }
    
    
    
    
    /**
     * 查询营运公司集合
     * @throws GSException
     */
    public List queryCompanyMessageList() throws GSException{
    	List ls = null;
        String tSql = "select * from t_warehouse where status=1 and is_show=1 and lengthb(code)=6";
        Object tObj[] = null;
        try{
            ls = executeQuery2(tSql,tObj);          
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return ls;
    }
    
    //查询营运公司税务信息
	public ArrayList executeQuery( String aSql,
			Object[] aStmntValues) throws SQLException, Exception {
		try {
			iConn = this.getConnetion();
			SQLLogger.log(aSql, aStmntValues);
			iPstmt = iConn.prepareStatement(aSql);

			// 组合Statement条件.
			if (aStmntValues != null) {
				GSPreparedStatementFactory.buildStatement(iPstmt, aStmntValues);
			}

			// 执行
			iRs = iPstmt.executeQuery();

			ArrayList tArrayList = new ArrayList();
			while (iRs.next()) {
				CompanyMessageData aBean= new CompanyMessageData();
				aBean.setId(iRs.getString("id"));
				aBean.setWarehouseCode(iRs.getString("code"));
				aBean.setWarehouseName(iRs.getString("name"));
				aBean.setCustomerName(iRs.getString("customer_name"));
				aBean.setCustomerTax(iRs.getString("customer_tax"));
				aBean.setCustomerAddress(iRs.getString("customer_address"));
				aBean.setCustomerPhone(iRs.getString("customer_phone"));
				aBean.setCustomerBand(iRs.getString("customer_band"));
				aBean.setCustomerUser(iRs.getString("customer_user"));
				tArrayList.add(aBean);
			}
			SQLLogger.log("Success");
			// 返回结果数组列表
			return tArrayList;
		} catch (SQLException e) {
			SQLLogger.log("error:" + e.toString());
			pushSQLError(e.toString());
			GSLogWriter.debug(this, e);
			GSLogWriter.debug(this, aSql);
			throw e;
		} finally {
			this.close();
		}
	}
	
	//营运公司集合
	
	public ArrayList executeQuery2( String aSql,
			Object[] aStmntValues) throws SQLException, Exception {
		try {
			iConn = this.getConnetion();
			SQLLogger.log(aSql, aStmntValues);
			iPstmt = iConn.prepareStatement(aSql);

			// 组合Statement条件.
			if (aStmntValues != null) {
				GSPreparedStatementFactory.buildStatement(iPstmt, aStmntValues);
			}

			// 执行
			iRs = iPstmt.executeQuery();

			ArrayList tArrayList = new ArrayList();
			while (iRs.next()) {
				Warehouse aBean = new Warehouse();
				aBean.setId(iRs.getString("id"));
				aBean.setCode(iRs.getString("code"));
				aBean.setName(iRs.getString("name"));
				aBean.setAddress(iRs.getString("address"));
				aBean.setStatus(iRs.getString("status"));
				tArrayList.add(aBean);
			}
			SQLLogger.log("Success");
			// 返回结果数组列表
			return tArrayList;
		} catch (SQLException e) {
			SQLLogger.log("error:" + e.toString());
			pushSQLError(e.toString());
			GSLogWriter.debug(this, e);
			GSLogWriter.debug(this, aSql);
			throw e;
		} finally {
			this.close();
		}
	}
	
	
	//查询营运公司税务
	public ArrayList executeQuery3( String aSql,
			Object[] aStmntValues) throws SQLException, Exception {
		try {
			iConn = this.getConnetion();
			SQLLogger.log(aSql, aStmntValues);
			iPstmt = iConn.prepareStatement(aSql);

			// 组合Statement条件.
			if (aStmntValues != null) {
				GSPreparedStatementFactory.buildStatement(iPstmt, aStmntValues);
			}

			// 执行
			iRs = iPstmt.executeQuery();

			ArrayList tArrayList = new ArrayList();
			while (iRs.next()) {
				CompanyMessageData aBean= new CompanyMessageData();
				aBean.setId(iRs.getString("id"));
				aBean.setWarehouseCode(iRs.getString("warehouse_code"));
				//aBean.setWarehouseName(iRs.getString("name"));
				aBean.setCustomerName(iRs.getString("customer_name"));
				aBean.setCustomerTax(iRs.getString("customer_tax"));
				aBean.setCustomerAddress(iRs.getString("customer_address"));
				aBean.setCustomerPhone(iRs.getString("customer_phone"));
				aBean.setCustomerBand(iRs.getString("customer_band"));
				aBean.setCustomerUser(iRs.getString("customer_user"));
				tArrayList.add(aBean);
			}
			SQLLogger.log("Success");
			// 返回结果数组列表
			return tArrayList;
		} catch (SQLException e) {
			SQLLogger.log("error:" + e.toString());
			pushSQLError(e.toString());
			GSLogWriter.debug(this, e);
			GSLogWriter.debug(this, aSql);
			throw e;
		} finally {
			this.close();
		}
	}
	
	public boolean update(String warehouseCode,CompanyMessageData cmd) throws GSException {
        StringBuffer Sql = new StringBuffer();
        Sql.append("update t_company_message set ");
        if(cmd.getCustomerName()!=null&&!cmd.getCustomerName().equals("")){
			Sql.append("customer_name='"+cmd.getCustomerName()+"', ");
		}
        if(cmd.getCustomerTax()!=null&&!cmd.getCustomerTax().equals("")){
			Sql.append("customer_tax='"+cmd.getCustomerTax()+"', ");
		}
        if(cmd.getCustomerAddress()!=null&&!cmd.getCustomerAddress().equals("")){
			Sql.append("customer_address='"+cmd.getCustomerAddress()+"', ");
		}
        if(cmd.getCustomerPhone()!=null&&!cmd.getCustomerPhone().equals("")){
			Sql.append("customer_phone='"+cmd.getCustomerPhone()+"', ");
		}
        if(cmd.getCustomerBand()!=null&&!cmd.getCustomerBand().equals("")){
			Sql.append("customer_band='"+cmd.getCustomerBand()+"', ");
		}
        if(cmd.getCustomerUser()!=null&&!cmd.getCustomerUser().equals("")){
			Sql.append("customer_user='"+cmd.getCustomerUser()+"', ");
		}
        Sql.append("warehouse_code='"+warehouseCode+"' ");
        Sql.append("where warehouse_code ='"+warehouseCode+"' ");
        String tSql = Sql.toString();
        Object[] tObject = null;
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
	
	public boolean insert(CompanyMessageData cmd) throws GSException {
        String tSql = "insert into t_company_message(id, warehouse_code, customer_name,customer_tax,customer_address,customer_phone,customer_band,customer_user) "+
        				"values(sys_guid(),'"+cmd.getWarehouseCode()+"','"+cmd.getCustomerName()+"','"+cmd.getCustomerTax()+"','"+cmd.getCustomerAddress()+"','"+cmd.getCustomerPhone()+"','"+cmd.getCustomerBand()+"','"+cmd.getCustomerUser()+"')";
        Object[] tObject = null;
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }

}
